package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user.devicedata;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.DeviceService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.DeviceService.ConnectionState;

/**
 * 
 * Este Bean es utilizado en las vistas
 * simpleuser/listdevices.xhtml
 * simpleuser/devicedata.xhtml
 */
public class DeviceDataBean {
	
	private DeviceService deviceService;
	private List<FieldBean> fieldsBean;
	private List<SimpleCommandBean> simpleCommandBeans;
	private List<CompoundCommandBean> compoundCommandBeans;
	private List<AlarmBean> alarmBeans;
	
	public DeviceDataBean(DeviceService dev) {
		deviceService = dev;
	}

	public DeviceService getDeviceService(){
		return this.deviceService;
	}
	public String getName(){
		return deviceService.getDeviceName();
		
	}
	
	public String getType(){
		return deviceService.getDeviceType();
		
	}
	
	public String getState(){
		ConnectionState state = deviceService.getDeviceState();
		if(state == ConnectionState.CONNECTED){
			return "devicedata.connectionstate.connected";
		}
		else if(state == ConnectionState.DISCONNECTED){
			return "devicedata.connectionstate.disconnected";
		}
		else if(state == ConnectionState.TRYING){
			return "devicedata.connectionstate.trying";
		}
		else if(state == ConnectionState.WAITING){
			return "devicedata.connectionstate.waiting";
		}
		return null;
		
	}
	
	public Date getLastdate(){
		return deviceService.getLastDate();
		
	}
	
	public String getBtnname(){
		ConnectionState state = deviceService.getDeviceState();
		if(state == ConnectionState.CONNECTED){
			return "devicedata.connectionbtn.disconnect";
		}
		else if(state == ConnectionState.DISCONNECTED){
			return "devicedata.connectionbtn.connect";
		}
		else if(state == ConnectionState.TRYING){
			return "devicedata.connectionbtn.connecting";
		}
		else if(state == ConnectionState.WAITING){
			return "devicedata.connectionbtn.wait";
		}
		return null;
	}
	
	public boolean isBtndisabled(){
		ConnectionState state = deviceService.getDeviceState();
		return state == ConnectionState.TRYING || state == ConnectionState.WAITING;
	}

	public void dolink(){
		//Object bean = PresentationUtil.getManagedBean("userSelectionBean");
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selectedDevice", this);
		/*
		if(bean instanceof UserSelectionBean){
			((UserSelectionBean) bean).setSelectedDevice(this);
		}
		*/
	}
	public String getDescription(){
		return deviceService.getDescription();
		
	}
	
	public Integer getSoundingtime(){
		return deviceService.getSoundingtime();
		
	}
	
	public String getActualizationtype(){
		boolean act = deviceService.getAutoActualization();
		if(!act){
			return "devicedata.actualizationtype.sounding";
		}
		else {
			return "devicedata.actualizationtype.automatic";
		}
		
	}

	public void pushbutton(){
		ConnectionState state = deviceService.getDeviceState();
		if(state == ConnectionState.CONNECTED){
			deviceService.disconnect();
		}
		else if(state == ConnectionState.DISCONNECTED){
			deviceService.connect();
		}
	}
	
	public List<FieldBean> getFields(){
		if(fieldsBean == null){
			fieldsBean = FieldBean.wrap(deviceService.getFieldsService());
		}
		return fieldsBean;
	}
	
	public List<SimpleCommandBean> getSimplecmds(){
		
		if(simpleCommandBeans == null){
			simpleCommandBeans = SimpleCommandBean.wrap(deviceService.getSimpleCommandsService());
		}
		return simpleCommandBeans;
	}
	
	public List<CompoundCommandBean> getCompundcmds(){
		if(compoundCommandBeans == null){
			compoundCommandBeans = CompoundCommandBean.wrap(deviceService.getCompoundCommandsService());
		}
		return compoundCommandBeans;
	}
	
	public List<AlarmBean> getAlarms(){
		if(alarmBeans == null){
			alarmBeans = AlarmBean.wrap(deviceService.getDeviceAlarmsService());
		}
		return alarmBeans;
	}
	
	public static List<DeviceDataBean> wrap(List<DeviceService> devices) {
		List<DeviceDataBean> ret = new ArrayList<DeviceDataBean>();
		for(DeviceService dev: devices){
			ret.add(new DeviceDataBean(dev));
		}
		return ret;
	}

	public Integer getID() {
		return deviceService.getDeviceID();
	}
	
	
}
